﻿using Rud.Holmgren.Tabulex3000.Util;
using Rud.Holmgren.TabulexLib;
using Rud.Holmgren.TabulexLib.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rud.Holmgren.Tabulex3000.CommunicationTasks
{
    class LoginTask : TbxTask
    {
        public LoginTask(TbxClient client)
            : base(client, "Logger ind")
        {
        }

        public override IPromise<bool> Run()
        {
            var retval = new Deferred<bool>();

            client.Login().Done(login =>
            {
                UpdateModel(login);
                retval.Resolve(true);
            }).Fail(retval.Reject);

            return retval;
        }

        private void UpdateModel(TbxLogin login)
        {
            // Update list of children

            foreach (var child in login.parent.children)
            {
                var task = new GetChildInfoTask(client, child);
                CommunicationManager.Instance.EnQueue(task);
            }
        }
    }
}
